package search;

public class BSearch {

    public static int bSearch(int[] a,int key){
        return bSearchInverse(a,key,0,a.length-1);
    }

    public static int bSearchInverse(int[] a,int key,int low,int high){
        if(low>high)return -1;
        int mid = low+(high-low)/2;
        if(key<a[mid]){
            bSearchInverse(a,key,low,mid-1);
        }else if(key>a[mid]){
            bSearchInverse(a,key,mid+1,high);
        }else if(key==a[mid]){
            return mid;
        }
        return -1;
    }

    public static void main(String[] args){
        int[] a = {1,2,3,4,5,6,7,8,9};
        int index = bSearch(a,5);
        System.out.printf("---("+index+")---");
    }
}
